import {
  curTimeAll
} from '../../third-utils/time/index.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
      recordDate:'',
        curCate:'',
        imageAttr:[],
        hxd_type:{},
        typeIndex:'',
        hxd_zz:{},
        zzIndex:'',
        hxd_cxsj:{},
        cxsjIndex:'',
        description:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(e) {

        
      this.setData({
        curCate:e.cateId,
        recordDate:curTimeAll
      })
          this.initData()
    },

    
    select(e){
      this.setData({
        [e.currentTarget.dataset.key]:e.currentTarget.dataset.index
      })
    },
    pickerChangeType(e){
      console.log(e,21)
      this.setData({
        [e.currentTarget.dataset.key]:e.detail.value
      })
    },
  formSubmit(e){
    console.log(e,12122)
    if(!e.detail.value.reason){
      wx.showToast({
          title: '请输入就诊原因',
          icon: 'none',
          duration: 1500 //持续的时间
      })
      return
  }
  if(!e.detail.value.diagnosis){
    wx.showToast({
        title: '请输入医生诊断',
        icon: 'none',
        duration: 1500 //持续的时间
    })
    return
}
    let post={
      babyId:32,
      typeId:this.data.curCate,
      
    }
      post.detail={
        recordDate:this.data.recordDate,
        reason:e.detail.value.reason,
        diagnosis:e.detail.value.diagnosis,
        treatmentPlan:e.detail.value.treatmentPlan,
        description:e.detail.value.description,
        picture:String(this.data.imageAttr)
      }
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/add',
      data:post,
      method: 'post',
      success:  (res)=> {
        console.log(res,122)
        if (res.data.code == 200) {
          
          wx.showToast({
            title: '保存成功！',
            icon: 'none',
            duration: 1500 //持续的时间
          })
          setTimeout(()=>{

            wx.navigateBack()
          },1500)
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
  },
    changeCate(e){
      this.setData({
        curCate:e.currentTarget.dataset.index
      })
    },
    delImg(e){
      this.data.imageAttr.splice(e.currentTarget.dataset.index,1)
      this.setData({
        imageAttr:this.data.imageAttr
      })
    },
    addImg(){
      wx.chooseImage({
        count:9,
        success: (chooseImageRes) => {
          console.log(chooseImageRes)
          const tempFilePaths = chooseImageRes.tempFilePaths;
          tempFilePaths.some((item)=>{
            wx.uploadFile({
              url: 'https://babyplanet.isv-dev.youzan.com/common/image/upload', //仅为示例，非真实的接口地址
              filePath:item,
              fileType: 'image',
              name: 'file',
              headers: {
                'Accept': 'application/json',
                'Content-Type': 'multipart/form-data',
              },
              success: (uploadFileRes) => {
                let res=JSON.parse(uploadFileRes.data)
                if(res.code==200){
                  this.data.imageAttr.push(res.data.imageUrl)
                  this.setData({
                    imageAttr:this.data.imageAttr
                  })
                }
              },
            });
          })
        }
      });
    },
    initData(){
        let codeAttr=['hxd_type','hxd_zz','hxd_cxsj']
        codeAttr.some(item=>{
            wx.request({
                url: 'https://babyplanet.isv-dev.youzan.com/api/selectConfig/view',
                data: {
                    code:item
                },
                method: 'get',
                success: (res) => {
                  if (res.data.code == 200) {
                    this.setData({
                        [item]:res.data.data
                    })
                  }
                }
              })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})